package com.dzf.controller;

import com.dzf.entity.*;
import com.dzf.service.BookService;
import com.dzf.service.LargeFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author DZF
 * 商品公共模块
 */
@RestController
@RequestMapping("/api/public")
public class PublicController {
    @Autowired
    BookService bookService;
    @Autowired
    LargeFileService largeFileService;


    /**
     * 商品的条件分页
     * @param start     商品起始页
     * @param num       每页商品数
     * @param condition 条件集合
     * @return  查询后的商品集合-->{books:商品的集合，‘total’：符合条件商品总数}
     */
    @RequestMapping("/book/findIf/{start}/{num}")
    public RespJson findIf(@PathVariable("start")int start,
                           @PathVariable("num")int num,
                           @RequestBody String condition){
        return RespJson.pack(200,"",bookService.findByIf(start, num, condition));
    }

    /**
     * 查询某一种类型的商品
     * @param start     起始页
     * @param num       页商品数
     * @param typeid    类型的ID
     * @return  分页后该类型的所有商品
     *          {books:商品的集合，‘total’：符合条件商品总数}
     */
    @RequestMapping("/findBookByPage/{start}/{num}/{typeid}")
    public RespJson findBookByPage(@PathVariable("start")int start,
                                   @PathVariable("num")int num,
                                   @PathVariable("typeid")int typeid){

        Map<String, Object> books = bookService.findBookByPage(start, num, typeid);
        return RespJson.pack(200,"",books);
    }


    /**
     * 点赞商品
     * @param bookid 被点赞的ID
     * @return 无返回值
     */
    @RequestMapping("/book/addRate/{bookid}")
    public RespJson addRate(@PathVariable("bookid")int bookid)
    {
        try {
            bookService.addRate(bookid);
        }catch (Exception e)
        {
            e.printStackTrace();
            return RespJson.pack(100,"未登录，失败",null);
        }
        return RespJson.pack(200,"点赞成功！",null);
    }

    /**
     * 根据 收藏 查询商品
     * @param start  商品起始页
     * @param num    每页商品数
     * @param typeid 商品类型
     * @return 获取 收藏 前五的商品
     *          {'books':排序后的商品}
     */
    @RequestMapping("findBookByPageSale/{start}/{num}/{typeid}")
    public RespJson findBookByPageSale(@PathVariable("start")int start,
                                   @PathVariable("num")int num,
                                   @PathVariable("typeid")int typeid){
        Map<String, Object> map = new HashMap<>();
        map.put("typeid",typeid);
        map.put("condition","salenum");

        //将已经打包完成的数据返回
        Map<String, Object> map1 = bookService.findBookByPageOrd(start, num, map);
        return RespJson.pack(200,"",map1);
    }


    /**
     * 根据点赞 获取商品
     * @param start  商品起始页
     * @param num    每页商品数
     * @param typeid 商品类型
     * @return 获取 点赞 前五的商品
     *          {'books':排序后的商品}
     */
    @RequestMapping("/findBookByPageRate/{start}/{num}/{typeid}")
    public RespJson findBookByPageRate(@PathVariable("start")int start,
                                   @PathVariable("num")int num,
                                   @PathVariable("typeid")int typeid){
        Map<String, Object> map = new HashMap<>();
        map.put("typeid",typeid);
        //将字段变为条件，在业务层使用动态条件去拼接sql
        map.put("condition","rate");

        //将已经打包完成的数据返回
        Map<String, Object> map1 = bookService.findBookByPageOrd(start, num, map);
        return RespJson.pack(200,"",map1);
    }

    /**
     * 根据阅读数 获取商品
     * @param start  商品起始页
     * @param num    每页商品数
     * @param typeid 商品类型
     * @return 获取 阅读 前五的商品
     *          {'books':排序后的商品}
     */
    @RequestMapping("/findBookByPageFav/{start}/{num}/{typeid}")
    public RespJson findBookByPageFav(@PathVariable("start")int start,
                                   @PathVariable("num")int num,
                                   @PathVariable("typeid")int typeid){
        Map<String, Object> map = new HashMap<>();
        map.put("typeid",typeid);
        map.put("condition","favnum");

        //将已经打包完成的数据返回
        Map<String, Object> map1 = bookService.findBookByPageOrd(start, num, map);
        return RespJson.pack(200,"",map1);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
